package com.zxy.lm.controller;


import com.zxy.lm.VO.AdminLoginVO;
import com.zxy.lm.entity.Admin;
import com.zxy.lm.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jiusi
 * @since 2021-06-22
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public String login(AdminLoginVO adminVO, HttpSession session, Model model) {
        Admin admin = adminService.login(adminVO);
        if (null == admin) {
            model.addAttribute("almsg", "账号或密码错误");
            return "admin/login";
        } else {
            session.setAttribute("admin", admin);
            return "admin/main";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "admin/login";
    }
}

